package com.hb.znpt.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.hb.znpt.bean.LandBean;
import com.hb.znpt.bean.LesseeBean;
import com.hb.znpt.model.LesseeModel;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.rxjava.rxlife.RxLife;
import com.unitlib.base.base.BaseViewModel;
import com.unitlib.base.utils.ToastUtils;
import com.xuexiang.xutil.net.NetworkUtils;

import org.litepal.LitePal;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * @Description: 描述
 * @Author: houbing
 * @CreateDate: 2021/12/17 0:20
 */
public class LesseeViewModel extends BaseViewModel<LesseeModel> {

    public MutableLiveData<List<LesseeBean>> listData = new MutableLiveData<>();

    public LesseeViewModel(@NonNull Application application) {
        super(application);
        model = new LesseeModel();
    }

    @Override
    public void onCreate() {
        XXPermissions.with(context)
                .permission(Permission.ACCESS_FINE_LOCATION)
                .permission(Permission.ACCESS_COARSE_LOCATION)
                .permission(Permission.ACCESS_BACKGROUND_LOCATION)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {

                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ToastUtils.showShort("被永久拒绝授权，请手动授权限");
                            XXPermissions.startPermissionActivity(context, permissions);
                        }
                    }
                });
        getData();
    }

    private void getData() {
        if (NetworkUtils.isHaveInternet()) {
            model.getData()
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(disposable -> {
                        getUC().getShowLoadingDialogEvent().call();
                    })
                    .doFinally(() -> {
                        getUC().getDismissLoadingDialogEvent().call();
                    })
                    .as(RxLife.as(owner))
                    .subscribe(beans -> {
                        if (model.getOffDataCount() == 0) {
                            for (int i = 0; i < beans.size(); i++) {
                                beans.get(i).save();
                            }
                        } else {
                            if (beans.size() == 0) {
                                LitePal.deleteAll(LandBean.class);
                            } else {
                                for (int i = 0; i < beans.size(); i++) {
                                    beans.get(i).saveOrUpdate("deptid = ?", beans.get(i).getDeptid());
                                }
                                List<LesseeBean> list = model.getOffData();
                                for (int i = 0; i < list.size(); i++) {
                                    boolean isHave = false;
                                    for (int j = 0; j < beans.size(); j++) {
                                        if (list.get(i).getDeptid().equals(beans.get(j).getDeptid())) {
                                            isHave = true;
                                        }
                                    }
                                    if (!isHave) {
                                        list.get(i).delete();
                                    }
                                }
                            }
                        }
                        listData.setValue(beans);
                    }, error -> {
                        if (error.getLocalizedMessage().equals("000006")) {
                            LitePal.deleteAll(LandBean.class);
                        } else {
                            ToastUtils.showShort(error.getMessage());
                            listData.setValue(model.getOffData());
                        }
                    });
        } else {
            listData.setValue(model.getOffData());
        }
    }
}
